package com.university.trade.service.impl;

import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.log4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import com.university.trade.entity.Blog;
import com.university.trade.entity.BlogType;
import com.university.trade.entity.Blogger;
import com.university.trade.entity.Link;
import com.university.trade.lucene.BlogIndex;
import com.university.trade.redis.RedisClient;
import com.university.trade.service.BlogService;
import com.university.trade.service.BlogTypeService;
import com.university.trade.service.BloggerService;
import com.university.trade.service.LinkService;
import com.university.trade.util.PropertiesUtil;

/**
 * 初始化组件 把博主信息 根据博客类别分类信息 根据日期归档分类信息 存放到application中，用以提供页面请求性能
 * @author Administrator
 *
 */
@Component
public class InitComponent implements ServletContextListener,ApplicationContextAware{
	private static Logger logger = Logger.getLogger(InitComponent.class);
	private static ApplicationContext applicationContext;
	@SuppressWarnings("static-access")
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		// TODO Auto-generated method stub
		this.applicationContext=applicationContext;
	}
	public void contextInitialized(ServletContextEvent servletContextEvent) {
		ServletContext application=servletContextEvent.getServletContext();
		BloggerService bloggerService=(BloggerService) applicationContext.getBean("bloggerService");
		Blogger blogger=bloggerService.find(); // 查询博主信息
		blogger.setPassword(null);
		application.setAttribute("blogger", blogger);
		
		BlogTypeService blogTypeService=(BlogTypeService) applicationContext.getBean("blogTypeService");
		List<BlogType> blogTypeCountList=blogTypeService.countList(); // 查询博客类别以及博客的数量
		application.setAttribute("blogTypeCountList", blogTypeCountList);
		
		BlogService blogService=(BlogService) applicationContext.getBean("blogService");
		List<Blog> blogCountList=blogService.countList(); // 根据日期分组查询博客
		application.setAttribute("blogCountList", blogCountList);
		PropertiesUtil systemProperties = new PropertiesUtil("/systemConfig.properties");
		String system_open_test = systemProperties.getProperty("system_open_test");//是否打开测试环境
		if(!"T".equals(system_open_test)){
			// 非测试环境 新增blog 索引文件
			List<Blog> blogList = blogService.fingAllBlog();
			BlogIndex blogIndex = new BlogIndex();
			for(Blog blog : blogList){
				try {
					blogIndex.addIndex(blog);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		LinkService linkService=(LinkService) applicationContext.getBean("linkService");
		List<Link> linkList=linkService.list(null); // 查询所有的友情链接信息
		application.setAttribute("linkList", linkList);
		//加载服务器资源配置方法一：可以从容器中取出实体对象redisClient,然后存放到redis里边
		RedisClient redisClient= (RedisClient) applicationContext.getBean("redisClient");
		logger.debug("----------------项目属性配置缓存 方法一 开始---------------------");
		redisClient.setCacheObject("system_open_test", system_open_test);
		System.out.println((String)redisClient.getCacheObject("system_open_test"));
		logger.debug("----------------项目属性配置缓存 方法一  结束---------------------");
		//加载服务器资源配置方法二：参考 com.university.trade.service.impl.StartAddCacheListener
	}

	public void contextDestroyed(ServletContextEvent sce) {
		
	}

}
